package com.usian.admin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.usian.admin.feign.ArticleFeign;
import com.usian.admin.feign.WemediaFeign;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.WemediaNewsAutoScanService;
import com.usian.common.aliyun.GreeTextScan;
import com.usian.common.aliyun.GreenImageScan;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.wemedia.vo.WmNewsVo;
import com.usian.utils.common.SensitiveWordUtil;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.applet.Main;

import java.io.IOException;
import java.util.*;

@Service
@Log4j2
public class WemediaNewsAutoScanServiceImpl implements WemediaNewsAutoScanService {
    @Autowired
    WemediaFeign wemediaFeign;

    @Override
    @GlobalTransactional
    public void autoScanByMediaNewsId(Integer id) {
        if (id==null){
            log.error("当前审核id为空");
            return;
        }
        //1根据id查询自媒体文章信息
        WmNews wmNews = wemediaFeign.findById(id);
        if (wmNews==null){
            log.error("自媒体文章不存在，自媒体的id",id);
            return;
        }
        //2.状态为4 人工审核通过 直接保存数据库创建索引
        if (wmNews.getStatus()==4){
            //保存数据
            saveAppArticle(wmNews);
            return;
        }
        //3 修改状态为8 发布时间>当前时间 直接保存数据
        if (wmNews.getStatus()==8&& wmNews.getPublishTime().getTime()<=System.currentTimeMillis()){
            //保存数据
            saveAppArticle(wmNews);
            return;
        }
        //4.文章状态为1 待审核
        if (wmNews.getStatus()==1){
            //抽取文章内容中的纯文本和图片
            Map<String,Object>  contentAndImagesResult= handleTextAndImages(wmNews);
            //4.1文本审核
            boolean textScanBoolean = handleTextScan((String) contentAndImagesResult.get("content"), wmNews);
            if (!textScanBoolean)return;
            //4.2 图片审核
            boolean imagesScanBoolean = handleImagesScan((List<String>) contentAndImagesResult.get("images"), wmNews);
            if (!imagesScanBoolean)return;
            //4.3自管理媒体审核
            boolean sensitiveScanBoolean= handleSensitive((String)contentAndImagesResult.get("content"),wmNews);
            if (!sensitiveScanBoolean)return;
            //4.4发布时间大于当前时间
            if (wmNews.getPublishTime().getTime()>System.currentTimeMillis()){
                //修改文章状态为8
                updateWmNews(wmNews,(short)8,"审核通过,待发布");
                return;
            }
            //5.审核通过，修改自媒体状态为9 保存app端相关文章信息
            saveAppArticle(wmNews);
        }

    }

    @Override
    public PageResponseResult findNews(NewsAuthDto dto) {
        //分页查询
        PageResponseResult responseResult = wemediaFeign.findList(dto);
        //有图片需要显示，需要fasfdfs服务器地址
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult findOne(Integer id) {
        //检查参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2查询数据
        WmNewsVo wmNewsVo = wemediaFeign.findWmNewsVo(id);
        //结构封装
        ResponseResult responseResult = ResponseResult.okResult(wmNewsVo);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult updateStatus(Integer type, NewsAuthDto dto) {
        //1.检查参数
        if(dto == null || dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询文章
        WmNews wmNews = wemediaFeign.findById(dto.getId());
        if(wmNews == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3审核没有通过
        if (type.equals(0)){
            updateWmNews(wmNews,(short)2,dto.getMsg());
        }else if (type.equals(1)){
            //4.人工审核通过
            updateWmNews(wmNews,(short)4,"人工审核通过");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    /**
     *
     /**
     * 敏感词审核
     *
     * @param content
     * @param wmNews
     * @return
     * **/

    private boolean handleSensitive(String content, WmNews wmNews) {
        boolean flag=true;
        List<String> allAdSensitive = adSensitiveMapper.findAllAdSensitive();
        //初始化敏感词
        SensitiveWordUtil.initMap(allAdSensitive);
        //文章自管理敏感词过滤
        Map<String, Integer> resultMap = SensitiveWordUtil.matchWords(content);
        if (resultMap.size()>0){
            log.error("敏感词过滤没有通过，包含了敏感词：{}",resultMap);
            //找到敏感词 审核不通过
            updateWmNews(wmNews,(short)2,"文章中包含敏感词");
            flag=false;
        }

        return flag;
    }

    @Autowired
    private GreenImageScan greenImageScan;
    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fdfs.url}")
    private  String fileServerUrl;
    /**
     * 审核图片
     *
     * @param images
     * @param wmNews
     * @return
     */
    private boolean handleImagesScan(List<String> images, WmNews wmNews) {
        if (images == null) {
            return true;
        }
        boolean flag = true;
        List<byte[]> imageList = new ArrayList<>();
        try {
            for (String image : images) {
                String imageName = image.replace(fileServerUrl, "");
                int index = imageName.indexOf("/");
                String groupName = imageName.substring(0, index);
                String imagePath = imageName.substring(index + 1);
                byte[] imageByte = fastDFSClient.download(groupName, imagePath);
                imageList.add(imageByte);
            }
            //阿里云图片审核
            Map map = greenImageScan.imageScan(imageList);
            //审核不通过
            if (!map.get("suggestion").equals("pass")) {
                //修改自媒体文章状态，并告知审核失败原因
                updateWmNews(wmNews, (short) 2, "文章中图片有违规");
                flag = false;
            }
            //人工审核
            if (map.get("suggestion").equals("review")) {
                //修改自媒体文章的状态，并告知审核失败原因
                updateWmNews(wmNews, (short) 3, "文章中有不确定元素");
                flag = false;
            }
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
    }
        return flag;
    }


    @Autowired
    private GreeTextScan greeTextScan;


    /**
     * 文本审核
     *
     * @param content
     * @param wmNews
     * @return
     */

    private boolean handleTextScan(String content, WmNews wmNews) {
        boolean flag=true;
        try {
            Map map = greeTextScan.greeTextScan(content);
            //审核不通过
            if (!map.get("suggestion").equals("pass")){
                //审核失败
                if (map.get("suggestion").equals("block")){
                    //修改自媒体文章的状态，并告知审核失败原因
                    updateWmNews(wmNews,(short)2,"文章内容中有敏感词汇");
                    flag=false;
                }
                //人工审核
                if (map.get("suggestion").equals("review")){
                    //修改自媒体文章的状态，并告知审核失败原因
                    updateWmNews(wmNews,(short)3,"文章内容中有不确定的词汇");
                    flag=false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return flag;
    }


    /**
     * 修改自媒体文章
     *
     * @param wmNews
     * @param status
     * @param msg
     */
    private void updateWmNews(WmNews wmNews, short status, String msg) {
        wmNews.setStatus(status);
        wmNews.setReason(msg);
        wemediaFeign.updateWmNews(wmNews);
    }

    /**
     * 提取文本内容和图片
     *
     * @param wmNews
     * @return
     */
    private Map<String, Object> handleTextAndImages(WmNews wmNews) {
        //文章内容
        String content = wmNews.getContent();
        //存储纯文本内容
        StringBuilder sb = new StringBuilder();
        //存储图片
        ArrayList<String> image = new ArrayList<>();
        List<Map> contentLst = JSONArray.parseArray(content, Map.class);
        for (Map map : contentLst) {
            if (map.get("type").equals("test")){
                sb.append(map.get("value"));
            }
            if (map.get("type").equals("image")){
                image.add((String)map.get("value"));
            }
            if (wmNews.getImages()!=null&&wmNews.getType()!=0){
                String[]split=wmNews.getImages().split(",");
                image.addAll(Arrays.asList(split));
            }
        }


        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("content",sb.toString());
        resultMap.put("images",image);
        return resultMap;

    }

    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    RestHighLevelClient restHighLevelClient;
    /**
     * 保存app文章相关的数据
     *
     * @param wmNews
     */
    private void saveAppArticle(WmNews wmNews) {
        //保存app文章
        ApArticle apArticle= saveArticle(wmNews);
        //保存app文章设置
        saveArticleConfig(apArticle);
        //保存文章内容
        saveArticleContent(apArticle,wmNews);
        //修改自媒体文章状态为9
        wmNews.setArticleId(apArticle.getId());
        updateWmNews(wmNews,(short)9,"审核通过");

        //TODD es索引创建saveAppArticle
        Map<String, Object> map = new HashMap<>();
        map.put("id",apArticle.getId().toString());
        map.put("publishTime",apArticle.getPublishTime());
        map.put("layout",apArticle.getLayout());
        map.put("images",apArticle.getImages());
        map.put("authorId",apArticle.getAuthorId());
        map.put("title",apArticle.getTitle());
        map.put("content",wmNews.getContent());
        //创建文档添加到索引库中
        org.elasticsearch.action.index.IndexRequest indexRequest = new IndexRequest("app_info_article").id(apArticle.getId().toString()).source(map);
        try {
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /**
     * 创建app端文章内容信息
     * @param apArticle
     * @param wmNews
     */
    private void saveArticleContent(ApArticle apArticle, WmNews wmNews) {
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(wmNews.getContent());
        articleFeign.saveArticleContent(apArticleContent);
    }

    /**
     * 创建app端文章配置信息
     * @param apArticle
     */
    private void saveArticleConfig(ApArticle apArticle) {
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(apArticle.getId());
        apArticleConfig.setIsForward(true);
        apArticleConfig.setIsDelete(false);
        apArticleConfig.setIsDown(true);
        articleFeign.saveArticleConfig(apArticleConfig);

    }
    @Autowired
    AdChannelMapper adChannelMapper;
    /**
     * 保存文章
     * @param wmNews
     * @return
     */
    private ApArticle saveArticle(WmNews wmNews) {
        ApArticle apArticle = new ApArticle();
        apArticle.setTitle(wmNews.getTitle());
        apArticle.setLayout(wmNews.getType());
        apArticle.setImages(wmNews.getImages());
        apArticle.setCreatedTime(new Date());

        //获取作者相关信息
        Integer userId = wmNews.getUserId();
        WmUser wmUser = wemediaFeign.findWmUserById(userId);
        if (wmUser!=null){
            String wmUserName = wmUser.getName();
            ApAuthor apAuthor = articleFeign.selectAuthorByName(wmUserName);
            if (apArticle!=null){
                apArticle.setAuthorId(apAuthor.getId().longValue());
                apArticle.setAuthorName(apAuthor.getName());
            }
        }
        //获取频道相关信息
        Integer channelId = wmNews.getChannelId();
        AdChannel adChannel = adChannelMapper.selectById(channelId);
        if (adChannel!=null){
            apArticle.setChannelId(adChannel.getId());
            apArticle.setChannelName(adChannel.getName());
        }
        return articleFeign.saveArticle(apArticle);
    }
}
